<script setup lang="tsx">
import { regionsListRegionApi } from '@/api/regions'
import { Province } from '@/api/regions/types'
import { pageBusinessApi } from '@/api/Integral'
import { ElButton } from 'element-plus'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useI18n } from '@/hooks/web/useI18n'
import { useTable } from '@/hooks/web/useTable'
import { useSearch } from '@/hooks/web/useSearch'
import { PointsMallStateDict, dictToList } from '@/utils/dict'
import { useRouter } from 'vue-router'

const { t } = useI18n()

const { push } = useRouter()

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { pageSize, currentPage } = tableState
    const res = await pageBusinessApi({
      pageNum: unref(currentPage),
      pageSize: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: res.data.list || [],
      total: res.data.total || 0
    }
  }
})
const { total, loading, dataList, pageSize, currentPage } = tableState
const { getList } = tableMethods

const { searchRegister, searchMethods } = useSearch()
const { setValues } = searchMethods

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'id',
    label: '商家ID',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'businessShortName',
    label: '商家简称',
    search: {
      componentProps: {
        placeholder: '请输入商家简称'
      }
    },
    form: {
      hidden: true
    },
    table: {
      width: 220,
      slots: {
        default: (data: any) => {
          const row = data.row as any
          return (
            <>
              <div class={'flex items-center'}>
                <img class={'w-10 mr-3'} src={row.logo} />
                <span>{row.businessShortName}</span>
              </div>
            </>
          )
        }
      }
    }
  },
  {
    field: 'businessName',
    label: '企业名称',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'pointsMallState',
    label: '状态',
    form: {
      hidden: true
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dictToList(PointsMallStateDict)
      }
    },
    table: {
      slots: {
        default: (data: any) => {
          const row = data.row as any
          return (
            <>
              <span>{row.pointsMallState && PointsMallStateDict[row.pointsMallState]}</span>
            </>
          )
        }
      }
    }
  },
  {
    field: 'productNum',
    label: '积分商品数',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    table: {
      width: 100
    }
  },
  {
    field: 'userNum',
    label: '积分用户数',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    table: {
      width: 100
    }
  },
  {
    field: 'totalPayPoints',
    label: '累计支出积分',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    table: {
      width: 100
    }
  },
  {
    field: 'totalExchangedPoints',
    label: '累计兑换积分',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    table: {
      width: 100
    }
  },
  {
    field: 'pointsMallFirstOpenTime',
    label: '开通时间',
    search: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetimerange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    },
    form: {
      hidden: true
    },
    table: {
      width: 220
    }
  },
  {
    field: 'action',
    label: t('userDemo.action'),
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    search: {
      hidden: true
    },
    table: {
      width: 250,
      slots: {
        default: (data) => {
          const row = data.row
          return (
            <>
              <ElButton type="primary" link onClick={() => goodsList(row)}>
                查看积分商品
              </ElButton>
            </>
          )
        }
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const searchParams = ref({})
const setSearchParams = (params: any) => {
  const { pointsMallFirstOpenTime, ...rest } = params
  let startTime, endTime
  if (pointsMallFirstOpenTime && pointsMallFirstOpenTime.length > 0) {
    startTime = pointsMallFirstOpenTime[0]
    endTime = pointsMallFirstOpenTime[1]
  }
  currentPage.value = 1
  searchParams.value = {
    startTime,
    endTime,
    ...rest
  }
  getList()
}

const goodsList = (row) => {
  push(`/Integral/goodsList?id=${row.id}`)
}
</script>

<template>
  <div class="flex w-100% h-100%">
    <ContentWrap class="flex-[3] ml-20px">
      <Search
        :schema="allSchemas.searchSchema"
        @reset="setSearchParams"
        @search="setSearchParams"
        @register="searchRegister"
      />
      <Table
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :columns="allSchemas.tableColumns"
        :data="dataList"
        :loading="loading"
        @register="tableRegister"
        :pagination="{
          total
        }"
      />
    </ContentWrap>
  </div>
</template>
